package com.kedacom.freedomLabel.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.base.BaseActivity;
import com.kedacom.freedomLabel.base.BaseViewModel;
import com.kedacom.freedomLabel.databinding.ActivitySetRestrictionBinding;
import com.kedacom.freedomLabel.model.bean.Config;
import com.kedacom.freedomLabel.model.bean.ConfigKeys;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.freedomLabel.model.db.DataCallback;
import com.kedacom.freedomLabel.model.db.DataLoader;
import com.kedacom.freedomLabel.model.db.dao.ConfigDao;
import com.kedacom.freedomLabel.utils.DistenUtils;
import com.kedacom.freedomLabel.utils.log.LLog;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.ViewModel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 设置上下限制的类
 */
@ContentView(R.layout.activity_set_restriction)
@ViewModel(BaseViewModel.class)
public class SetRestrictionActivity extends BaseActivity<ActivitySetRestrictionBinding, BaseViewModel> {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ConfigDao dao = AppDatabase.getInstance().configDao();
        DataLoader.get(dao, ConfigKeys.upper, new DataCallback<List<Config>>() {
            @Override
            public void success(@Nullable List<Config> data) {
                if (data != null && !data.isEmpty()) {
                    String configValue = data.get(0).configValue;
                    mBinding.restrictionUpper.setContentText(configValue);
                } else {
                    showToast("未配置重量上限", TOAST_WARNING);
                    LLog.debug("未配置重量上限");
                }
            }

            @Override
            public void error(@Nullable String errorMessage) {
                showToast("重量上限初始化失败", TOAST_ERROR);
                LLog.error("重量上限初始化失败");
            }
        });

        DataLoader.get(dao, ConfigKeys.low, new DataCallback<List<Config>>() {
            @Override
            public void success(@Nullable List<Config> data) {
                if (data != null && !data.isEmpty()) {
                    String configValue = data.get(0).configValue;
                    mBinding.restrictionLower.setContentText(configValue);
                } else {
                    showToast("未配置重量下限", TOAST_WARNING);
                    LLog.debug("未配置重量下限");
                }
            }

            @Override
            public void error(@Nullable String errorMessage) {
                showToast("重量下限初始化失败", TOAST_ERROR);
                LLog.error("重量下限初始化失败");
            }
        });
    }

    public void onItemSaveButtonClick(View view) {
        beep();
        String lowerContentText = mBinding.restrictionLower.getContentText();
        String upperContentText = mBinding.restrictionUpper.getContentText();

        if (TextUtils.isEmpty(lowerContentText)) {
            showToast("下限不能为空", TOAST_ERROR);
        } else if (TextUtils.isEmpty(upperContentText)) {
            showToast("上限不能为空", TOAST_ERROR);
        } else if (!DistenUtils.isMoreThanMost(new BigDecimal(upperContentText), new BigDecimal(lowerContentText))) {
            showToast("上限必须大于下限", TOAST_ERROR);
        } else {
            ConfigDao dao = AppDatabase.getInstance().configDao();
            List<Config> data = new ArrayList<>();
            data.add(new Config(ConfigKeys.upper, upperContentText));
            data.add(new Config(ConfigKeys.low, lowerContentText));
            DataLoader.save(dao, data, new DataCallback<List<Config>>() {
                @Override
                public void success(@Nullable List<Config> data) {
                    finish();
                }

                @Override
                public void error(@Nullable String errorMessage) {
                    showToast("保存失败", TOAST_ERROR);
                    LLog.error("重量上下限保存失败");
                }
            });
        }
    }

    public void onBackClick(View view) {
        beep();
        finish();
    }
}